var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');
var config = require('../../config');
var util = require('../../utils/util.js')

var app = getApp()
Page({
  data: {
    intoRoom: config.service.intoRoom,
    userInfo: {},
    room_id: '',
    room_name: '',
    chip: '',
    buy_chip_count: 0,
    isLoading: false
  },
  bindKeyInput: function(e) {
    this.setData({
      buy_chip_count: e.detail.value
    })
  },
  createRoom: function() {
    var self = this;
    self.setData({
      isLoading: true
    });
    qcloud.request({
      url: self.data.intoRoom,
      data: {
         room_id: self.data.room_id,
         buy_chip_count: self.data.buy_chip_count
      },
      login: true,
      success(resp) {
        self.setData({
          isLoading: false
        });
        if(resp.data.code === 0){
              wx.redirectTo({
                url: '../details/details?room_id='+ self.data.room_id + '&buy_chip_count=' + self.data.buy_chip_count +'&buy_chip=' + resp.data.data.buy_chip + '&room_name=' + self.data.room_name+ '&chip=' + self.data.chip
              });
          }else{
              util.showSuccess('suucess',resp.data.message);
          }
      },
      fail(error) {
          console.log('request fail');
      },
      complete() {
          console.log('request complete');
      }
    });
  },
  onLoad: function (options) {
    console.log(options);
    var self = this;
    self.setData({
      room_id: options.room_id,
      room_name: options.room_name,
      chip: options.chip
    });
    wx.setNavigationBarTitle({
      title: self.data.room_name + ' 的房间'
    });
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      self.setData({
        userInfo:userInfo
      })
    })
  }
})
